package com.webdrider.servlet.util.parser.blog;

import com.common.model.abstracts.AbstractResponse;
import com.common.security.exceptions.ParserException;
import com.common.servlet.abstracts.AbstractJSONParser;
import com.webdrider.model.response.blog.GetAllCategoriesResponse;
import com.webdrider.servlet.util.JSONProtocol;

public class GetAllCategoriesJSONParser extends AbstractJSONParser {

	@Override
	public String parseResponse(AbstractResponse abstractResponse) throws ParserException {
		GetAllCategoriesResponse response;
		
		if (abstractResponse instanceof GetAllCategoriesResponse) {
			response = (GetAllCategoriesResponse) abstractResponse;
		} else {
			throw new ParserException(ParserException.RESPONSE_MATCH_ERROR);
		}
		
		StringBuilder builder = new StringBuilder();
		
		builder.append(JSONProtocol.OPEN_TAG);
		builder.append(JSONProtocol.ACTION_TAG);
		builder.append(valueToString(response.getRequestAction()));
		builder.append(JSONProtocol.NEW_PARAMETER);
		builder.append(JSONProtocol.DATA_TAG);
		builder.append(valueToArray(response.getCategories()));
		builder.append(JSONProtocol.CLOSE_TAG);
		
		return builder.toString();
	}
}
